/**
 * Copyright (c) 2016-2019 ykf All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.basic.modules.sys.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.basic.common.annotation.StringTrim;
import com.basic.common.annotation.SysLog;
import com.basic.common.exception.RRException;
import com.basic.common.utils.Constant;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.R;
import com.basic.common.validator.ValidatorUtils;
import com.basic.modules.sys.entity.SysRoleEntity;
import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.service.SysRoleMenuService;
import com.basic.modules.sys.service.SysRoleService;
import com.basic.modules.sys.service.SysUserRoleService;
import com.basic.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色管理
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	@Autowired
	private SysUserRoleService sysUserRoleService;

	@Autowired
	private SysUserService sysUserService;

	/**
	 * 角色列表
	 */
	@StringTrim
	@PostMapping("/list")
	public R list(@RequestBody ParamsModel<SysRoleEntity> paramsModel){
		//如果不是超级管理员，则只查询自己创建的角色列表
		if(getUserId() != Constant.SUPER_ADMIN){
			paramsModel.getCondition().setCreateUserId(getUserId());
		}
		PageUtils page = sysRoleService.queryPage(paramsModel);
		return R.ok(page);
	}

	/**
	 * 角色列表
	 */
	@StringTrim
	@PostMapping("/select")
	public R select(){
		Map<String, Object> map = new HashMap<>();
		//如果不是超级管理员，则只查询自己所拥有的角色列表
		if(getUserId() != Constant.SUPER_ADMIN){
			map.put("create_user_id", getUserId());
		}
		List<SysRoleEntity> list = (List<SysRoleEntity>) sysRoleService.listByMap(map);

		return R.ok(list);
	}

	/**
	 * 角色信息
	 */
	@PostMapping("/info")
	public R info(@RequestBody SysRoleEntity param){
		if (ObjectUtil.isEmpty(param) || ObjectUtil.isEmpty(param.getRoleId())) {
			throw new RRException("参数缺失");
		}
		SysRoleEntity role = sysRoleService.getById(param.getRoleId());
//		if (ObjectUtil.isNotEmpty(role.getCompanyPerms())) {
//			role.setCompanyPermList(StrUtil.split(role.getCompanyPerms(), ','));
//		}
		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(param.getRoleId());
		role.setMenuIdList(menuIdList);
		return R.ok(role);
	}

	/**
	 * 保存角色
	 */
	@SysLog("保存角色")
	@PostMapping("/save")
	public R save(@RequestBody SysRoleEntity role){
		ValidatorUtils.validateEntity(role);

		role.setCreateUserId(getUserId());
//		if (ObjectUtil.isNotEmpty(role.getCompanyPermList())) {
//			role.setCompanyPerms(StrUtil.join("," , role.getCompanyPermList()));
//		}
		sysRoleService.saveRole(role);

		return R.ok();
	}

	/**
	 * 修改角色
	 */
	@SysLog("修改角色")
	@PostMapping("/update")
	public R update(@RequestBody SysRoleEntity role){
		ValidatorUtils.validateEntity(role);
//		if (ObjectUtil.isNotEmpty(role.getCompanyPermList())) {
//			role.setCompanyPerms(StrUtil.join("," , role.getCompanyPermList()));
//		}
		role.setCreateUserId(getUserId());
		sysRoleService.update(role);

		return R.ok();
	}

	/**
	 * 删除角色
	 */
	@SysLog("删除角色")
	@PostMapping("/delete")
	public R delete(@RequestBody Long[] roleIds){
		sysRoleService.deleteBatch(roleIds);
		return R.ok();
	}

	/**
	 * 查询已分配用户
	 */
	@StringTrim
	@SysLog("查询已分配用户")
	@PostMapping("/searchDispatchUser")
	public R searchDispatchUser(@RequestBody SysRoleEntity role){
		List<Long> sysUserList = sysUserRoleService.searchDispatchUser(role);
		return R.ok(sysUserList);
	}

	/**
	 * 分配用户
	 */
	@SysLog("分配用户")
	@PostMapping("/dispatchUser")
	public R dispatchUser(@RequestBody SysRoleEntity role){
		sysUserRoleService.saveUserRole(role);
		return R.ok();
	}

	/**
	 * 授权角色
	 */
	@SysLog("授权角色")
	@PostMapping("/dispatchRole")
	public R dispatchRole(@RequestBody SysUserEntity user){
		if (ObjectUtil.isEmpty(user) || ObjectUtil.isEmpty(user.getUserId())) {
			throw new RRException("参数缺失");
		}
		sysUserService.dispatchRole(user);
		return R.ok();

	}

	/**
	 * 查询授权角色
	 */
	@StringTrim
	@SysLog("查询授权角色")
	@PostMapping("/searchDispatchRole")
	public R searchDispatchRole(@RequestBody SysUserEntity user){
		if (ObjectUtil.isEmpty(user) || ObjectUtil.isEmpty(user.getUserId())) {
			throw new RRException("参数缺失");
		}
		List<SysRoleEntity> roleList = sysUserService.searchDispatchRole(user);
		if (ObjectUtil.isNotEmpty(roleList)) {
			return R.ok(roleList.stream().map(SysRoleEntity::getRoleId).collect(Collectors.toList()));
		}
		return R.ok(new ArrayList<>());
	}
}
